<template>
  <view class="address-container">
    <!-- 地址列表 -->
    <view v-if="addressList.length > 0">
      <view v-for="(address, index) in addressList" :key="index" class="address-item">
        <view class="address-content" @click="selectAddress(index)">
          <view class="radio-wrapper">
            <view class="radio" :class="{selected: selectedIndex === index}">
              <view class="radio-inner" v-if="selectedIndex === index"></view>
            </view>
          </view>
          <view class="address-info">
            <view class="top-line">
              <text class="name">{{ address.name }}</text>
              <text class="phone">{{ address.phone }}</text>
              <text class="default-tag" v-if="address.isDefault">默认</text>
            </view>
            <text class="detail-address">
              {{ address.province }}{{ address.city }}{{ address.district }}{{ address.detailAddress }}
            </text>
          </view>
        </view>
        <view class="action-buttons">
          <view class="edit-btn" @click.stop="editAddress(index)">编辑</view>
          <view class="delete-btn" @click.stop="deleteAddress(index)">删除</view>
        </view>
      </view>
    </view>
    <!-- 无地址时的提示 -->
    <view v-else class="empty-tip">
      <image src="/static/no-address.png" class="empty-image"></image>
      <text class="empty-text">暂无收货地址</text>
    </view>
    <!-- 新建地址按钮 -->
    <button class="add-address-btn" @click="addAddress">+ 新建收货地址</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      addressList: [],
      selectedIndex: null,
      fromPage: '' // 记录从哪个页面跳转过来
    };
  },
  onLoad(options) {
    // 获取来源页面信息
    this.fromPage = options.from || '';
    this.loadAddressList();
  },
  onShow() {
    this.loadAddressList();
  },
  methods: {
    loadAddressList() {
      const savedAddresses = uni.getStorageSync('addressList') || [];
      this.addressList = savedAddresses;
    },
    goBack() {
      uni.navigateBack();
    },
    selectAddress(index) {
      // 将所选地址设为默认地址，其他地址取消默认
      this.addressList.forEach((item, i) => {
        item.isDefault = i === index;
      });
      uni.setStorageSync('addressList', this.addressList);

      this.selectedIndex = index;

      // 如果是从订单页面跳转过来的，选择后自动返回
      if (this.fromPage === 'confirm') {
        this.confirmSelection();
      }
    },
    addAddress() {
      uni.navigateTo({
        url: '/pages/addAddressnew/addAddressnew'
      });
    },
    editAddress(index) {
      const address = this.addressList[index];
      uni.navigateTo({
        url: `/pages/addAddressnew/addAddressnew?edit=true&id=${address.id}&name=${address.name}&phone=${address.phone}&province=${address.province}&city=${address.city}&district=${address.district}&detailAddress=${address.detailAddress}&isDefault=${address.isDefault}`
      });
    },
    deleteAddress(index) {
      uni.showModal({
        title: '提示',
        content: '确定要删除这个地址吗？',
        success: (res) => {
          if (res.confirm) {
            this.addressList.splice(index, 1);
            uni.setStorageSync('addressList', this.addressList);
            if (this.selectedIndex === index) {
              this.selectedIndex = null;
            }
            // 检查删除后是否还有默认地址，如果没有则将第一个地址设为默认地址
            const defaultIndex = this.addressList.findIndex(item => item.isDefault);
            if (defaultIndex === -1 && this.addressList.length > 0) {
              this.addressList[0].isDefault = true;
              uni.setStorageSync('addressList', this.addressList);
            }
            uni.showToast({
              title: '删除成功',
              icon: 'success'
            });
          }
        }
      });
    },
    confirmSelection() {
      if (this.selectedIndex!== null) {
        const selectedAddress = this.addressList[this.selectedIndex];

        // 使用全局事件传递选中的地址
        uni.$emit('addressSelected', selectedAddress);

        // 也可以使用本地存储传递
        uni.setStorageSync('selectedAddress', selectedAddress);

        uni.navigateBack();
      } else {
        uni.showToast({
          title: '请选择一个地址',
          icon: 'none'
        });
      }
    }
  }
};
</script>

<style scoped>
.address-container {
  padding: 15px;
  background-color: #f9f9f9;
  min-height: 100vh;
  padding-bottom: 80px;
}

.address-item {
  background-color: white;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 15px;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.05);
}

.address-content {
  display: flex;
  align-items: flex-start;
  margin-bottom: 12px;
}

.radio-wrapper {
  margin-right: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.radio {
  width: 20px;
  height: 20px;
  border: 1px solid #ccc;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.radio.selected {
  border-color: #007AFF;
}

.radio-inner {
  width: 12px;
  height: 12px;
  background-color: #007AFF;
  border-radius: 50%;
}

.address-info {
  flex: 1;
}

.top-line {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.name, .phone {
  font-size: 16px;
  color: #333;
  margin-right: 15px;
  font-weight: 500;
}

.phone {
  color: #666;
}

.default-tag {
  font-size: 12px;
  color: #ff6a00;
  background-color: #fff5e6;
  padding: 2px 6px;
  border-radius: 4px;
  margin-left: 10px;
}

.detail-address {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
  display: block;
}

.action-buttons {
  display: flex;
  justify-content: flex-end;
  border-top: 1px solid #f0f0f0;
  padding-top: 12px;
  margin-top: 12px;
}

.edit-btn {
  padding: 6px 16px;
  border-radius: 4px;
  font-size: 14px;
  color: #007AFF;
  background-color: #e6f7ff;
  border: 1px solid #007AFF;
  margin-right: 8px;
}

.delete-btn {
  padding: 6px 16px;
  border-radius: 4px;
  font-size: 14px;
  color: #ff4d4f;
  background-color: #fff1f0;
  border: 1px solid #ff4d4f;
}

.empty-tip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-top: 60px;
}

.empty-image {
  width: 120px;
  height: 120px;
  margin-bottom: 20px;
  opacity: 0.6;
}

.empty-text {
  font-size: 14px;
  color: #999;
}

.add-address-btn {
  position: fixed;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  background-color: #007AFF;
  color: white;
  padding: 12px;
  border: none;
  border-radius: 25px;
  width: 90%;
  max-width: 400px;
  font-size: 16px;
  box-shadow: 0 2px 10px rgba(0, 122, 255, 0.2);
}
</style>